<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form ref="queryForm" :inline="true" :model="queryParams" label-width="120px" size="small">
        <el-form-item prop="params.paymentNo">
          <el-input
            v-model="queryParams.params.paymentNo"
            class="filter-item"
            clearable
            placeholder="订单号"
          />
        </el-form-item>
        <el-form-item prop="params.commodityName">
          <el-input
            v-model="queryParams.params.commodityName"
            class="filter-item"
            clearable
            placeholder="商品名称"
          />
        </el-form-item>
        <el-form-item prop="params.memberId">
          <el-input
            v-model="queryParams.params.memberId"
            class="filter-item"
            clearable
            placeholder="会员ID"
          />
        </el-form-item>
        <el-form-item prop="params.payType">
          <el-select v-model="queryParams.params.payType" clearable placeholder="支付类型">
            <el-option v-for="item in payTypeList" :key="item.id" :label="item.value" :value="item.code" />
          </el-select>
        </el-form-item>
        <el-form-item prop="payDate">
          <el-date-picker
            v-model="dateTime"
            end-placeholder="结束日期"
            range-separator="至"
            start-placeholder="开始日期"
            style="width: 100%"
            type="datetimerange"
            @change="queryTimeChangeHandler"
          />
        </el-form-item>
        <!-- 按钮 -->
        <el-button
          v-permission="['/store/sale/search']"
          v-waves
          class="filter-item"
          icon="el-icon-search"
          size="small"
          style="margin-left: 10px;"
          type="primary"
          @click="getDataList"
        >查询
        </el-button>
        <el-button
          v-permission="['/store/sale/search']"
          v-waves
          class="filter-item"
          icon="el-icon-refresh"
          size="small"
          type="primary"
          @click="resetQueryForm"
        >重置
        </el-button>
        <el-form-item style="position: absolute; right: 2%">
          <el-tooltip class="item" content="自定义字段" effect="dark" placement="top-start">
            <el-popover
              placement="bottom"
              title="自定义字段"
              trigger="click"
              width="500"
            >
              <el-collapse v-model="activeNames">
                <el-collapse-item name="1" title="基本属性">
                  <div
                    v-for="item in defaultColumns"
                    :key="item.prop"
                    :label="item"
                    style="display: inline-block;padding-left: 20px;padding-top: 10px"
                  >
                    <el-checkbox v-model="item.isShow" @change="changeColumns(item)">
                      {{ item.label }}
                    </el-checkbox>
                  </div>
                </el-collapse-item>
              </el-collapse>
              <el-button
                slot="reference"
                style="position: absolute; right: 2%"
                type="success"
              >
                自定义字段
              </el-button>
            </el-popover>
          </el-tooltip>
        </el-form-item>
      </el-form>
    </div>
    <!-- 数据表格 -->
    <el-table
      v-loading="dataListLoading"
      :data="dataList"
      border
      data-list
      fit
      highlight-current-row
      size="small"
      style="width: 100%;"
      @sort-change="handleSortChange"
    >
      <el-table-column :index="shownIndex" align="center" fixed label="序号" type="index" />
      <el-table-column
        v-for="(item,index) in defaultColumns"
        v-if="item.isShow===true"
        :key="index"
        :label="item.label"
        :prop="item.prop"
        :sortable="item.custom"
        show-overflow-tooltip
      />
    </el-table>
    <!-- 分页组件 -->
    <div class="pagination-container">
      <el-pagination
        :current-page="queryParams.page"
        :page-size="queryParams.size"
        :page-sizes="[10,20,30, 50]"
        :total="total"
        background
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>
<script>
import { fetchList } from '@/api/store/sale'
import { typeList } from '@/api/system/dict'
import { getTemplate } from '@/api/coupon/template'

var moment = require('moment')
export default {
  name: 'Record',
  data() {
    return {
      total: 0,
      couponTypeList: [],
      templateList: [], // 可用券列表
      dateTime: [],
      dataList: [],
      payTypeList: [],
      activeNames: ['1'],
      queryTime: '',
      dataListLoading: true,
      dialogStatus: '',
      dialogLoading: false,
      dialogTitleMap: {
        add: '新增',
        update: '编辑'
      },
      // 查询参数对象
      queryParams: {
        page: 1,
        size: 10,
        params: {
          paymentNo: '',
          commodityName: '',
          memberId: '',
          payType: '',
          payDate1: '',
          payDate2: ''
        }
      },
      defaultColumns: [
        { prop: 'memberId', label: '会员ID', isShow: true, custom: true },
        { prop: 'memberName', label: '会员名称', isShow: true, custom: true },
        { prop: 'paymentNo', label: '订单号', isShow: true, custom: false },
        { prop: 'commodityName', label: '商品名称', isShow: true, custom: false },
        { prop: 'amount', label: '应付金额', isShow: true, custom: true },
        { prop: 'payAmount', label: '支付金额', isShow: true, custom: true },
        { prop: 'salePrice', label: '销售价', isShow: false, custom: true },
        { prop: 'costPrice', label: '成本价', isShow: false, custom: true },
        { prop: 'quantity', label: '商品数量', isShow: false, custom: true },
        { prop: 'profitAmount', label: '利润', isShow: false, custom: true },
        { prop: 'payDate', label: '支付时间', isShow: false, custom: true },
        { prop: 'payTypeName', label: '支付类型', isShow: false, custom: false },
        { prop: 'couponDiscount', label: '优惠券金额', isShow: false, custom: true },
        { prop: 'refundDate', label: '退款时间', isShow: false, custom: true },
        { prop: 'pointAmount', label: '积分数量', isShow: false, custom: true },
        { prop: 'createdDate', label: '创建时间', isShow: false, custom: true },
        { prop: 'creatorName', label: '创建人', isShow: false, custom: false },
        { prop: 'createdDate', label: '创建时间', isShow: false, custom: true },
        { prop: 'modifierName', label: '编辑人', isShow: false, custom: false },
        { prop: 'modifiedDate', label: '修改时间', isShow: false, custom: false },
        { prop: 'remark', label: '备注', isShow: false, custom: false }
      ]
    }
  },
  computed: {
    formReadOnly() {
      return this.dialogStatus === 'view'
    }
  },
  mounted() {
    typeList({ type: 'couponType' }).then(res => {
      this.couponTypeList = res.data
    })
    typeList({ type: 'payType' }).then(res => {
      this.payTypeList = res.data
    })
    getTemplate().then(res => {
      this.templateList = res.data
    })
    this.getDataList()
    // 首次进入页面时查找local中是否有存储的显示列
    const str = localStorage.getItem('saleColumns')
    if (str !== null) {
      this.defaultColumns = JSON.parse(str)
    }
  },
  methods: {
    shownIndex(index) {
      return (this.queryParams.page - 1) * this.queryParams.size + index + 1
    },
    handleSortChange(data) {
      console.log('handleSortChange')
      console.log(data)
      if (data.order) {
        if (data.order.indexOf('asc') > -1) {
          this.queryParams.sortOrder = 'asc'
        } else {
          this.queryParams.sortOrder = 'desc'
        }
        this.queryParams.sortName = data.prop
      }
      console.log(this.queryParams)
      this.getDataList()
    },
    resetQueryForm() {
      this.$refs['queryForm'].resetFields()
      this.queryParams.page = 1
      this.dateTime = []
      this.queryParams.params.payDate1 = ''
      this.queryParams.params.payDate2 = ''
      this.getDataList()
    },
    getDataList() {
      this.dataListLoading = true
      fetchList(this.queryParams)
        .then(res => {
          console.log(res)
          this.dataList = res.data.records
          this.total = res.data.total
          this.dataListLoading = false
          this.$message.success(res.msg || '获取数据成功')
        })
        .catch(err => {
          console.log(err)
          this.dataListLoading = false
        })
    },
    handleSizeChange(size) {
      this.queryParams.size = size
      this.getDataList()
    },
    handleCurrentChange(page) {
      this.queryParams.page = page
      this.getDataList()
    },
    queryTimeChangeHandler(value) {
      if (value) {
        this.queryParams.params.payDate1 = value[0]
        this.queryParams.params.payDate2 = value[1]
      }
    },
    // 更改表格的显示列时存储到location中
    changeColumns(val) {
      const num = this.defaultColumns.filter(p => {
        return p.isShow
      })
      if (num.length > 10) {
        this.$message.error('最多选中10个字段')
        // eslint-disable-next-line no-return-assign
        return val.isShow = false
      }
      localStorage.setItem('saleColumns', JSON.stringify(this.defaultColumns))
    }
  }
}
</script>
<style scoped>

.el-form-item {
  margin-bottom: 30px;
}

</style>
